Caramelize your onions: Heat butter and olive oil in a large, deep saute pan, ideally one that’s ovenproof, over medium heat. Add the onions, toss to coat them in butter and oil and cover the pot. Reduce the heat to medium-low and let the onions slowly steep for 15 to 20 minutes — they don’t need your attention. You want to start the stirring process sooner if they’re browning or sticking to the pan. Uncover the pot, raise the heat to medium-high (for now) and stir in salt — I start with 2 teaspoons of kosher salt. Cook onions, stirring every few minutes (you can check less often in the beginning, and will have to frequently stir later in the process) until the onions are brown, soft, and sweet, about 20 minutes more. You’ll want to reduce the heat to medium once any water the onions emitted has cooked off.
Heat your oven: To 375°F (190°C).
Assemble casserole: Add garlic to the onions and cook, stirring, one minute. Add sherry, if using, and cook until it disappears. Add the farro and cook for 2 minutes. Add lentils and 6 cups broth and thyme and bring the mixture to a simmer. Season well with salt and pepper as needed. If your pan isn’t ovenproof, transfer it at this point to a casserole dish.
Bake the casserole: Cover the pan and bake, stirring about halfway through, until the lentils and farro are tender, 40 to 50 minutes. If all of the liquid has absorbed, either at the midway through point or at the end, add another ¼ to ½ cup splash of broth. We’re looking for a risotto-like looseness or light “slosh” in the pan. Fish out thyme stems.
To finish: Sprinkle the dish with gruyere and broil until browned on top.
Do ahead: The dish keeps fantastically in the fridge for 4 to 5 days; rewarm in a 350-degree oven. You can also prepare the lentil and farro base a few days early, rewarm it (it will likely need more broth at this point), and broil the cheese on top right before serving.