01 - Preheat oven to 375°F. Generously grease a 9x13-inch baking dish with butter or cooking spray.
02 - Melt 2 tablespoons butter in a large skillet over medium heat. Add sliced onions and a pinch of salt. Cook, stirring frequently, until onions are deeply golden and caramelized, about 20-25 minutes. Add minced garlic during the last minute and stir until fragrant. Remove from heat.
03 - In a small saucepan, combine heavy cream, whole milk, dried thyme, nutmeg, black pepper, and remaining 1 tablespoon butter. Heat over medium-low heat until steaming but not boiling. If using flour for thickening, whisk it in until completely smooth and slightly thickened.
04 - Arrange half of the sweet potato slices in an overlapping pattern in the prepared baking dish. Season lightly with salt.
05 - Spread half of the caramelized onions evenly over the potatoes. Sprinkle with half of the Gruyère and half of the cheddar cheese.
06 - Repeat layering with remaining sweet potatoes, caramelized onions, and cheeses. Finish with a generous layer of cheese on top.
07 - Carefully pour the warm cream mixture evenly over the entire dish, ensuring it reaches all layers.
08 - Cover the baking dish tightly with aluminum foil. Bake for 40 minutes to allow potatoes to cook through.
09 - Remove foil and continue baking uncovered for 20 minutes, or until the top is golden brown and bubbly, and potatoes are fork-tender when pierced.
10 - Let the gratin rest for 10 minutes before serving to allow the layers to set. Garnish with chopped fresh chives if desired.