01 - Preheat oven to 375°F. Grease a 9x13-inch baking dish with butter or cooking spray.
02 - Melt 2 tbsp 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 and cook for 1 minute more. Remove from heat.
03 - Combine heavy cream, whole milk, dried thyme, ground nutmeg, black pepper, and remaining 1 tbsp butter in a small saucepan. Heat over medium-low until steaming but not boiling. If using flour for thickening, whisk in until smooth.
04 - Arrange half of the sweet potato slices in the prepared baking dish, overlapping slightly. Season lightly with salt.
05 - Spread half of the caramelized onions 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 remaining cheeses.
07 - Pour the warm cream mixture evenly over the entire layered dish, ensuring it seeps through all layers.
08 - Cover tightly with aluminum foil and bake for 40 minutes until potatoes begin to soften.
09 - Remove foil and continue baking for 20 minutes until the top is golden brown and bubbly, and potatoes are fork-tender.
10 - Let gratin rest for 10 minutes before serving to allow layers to set. Garnish with chopped fresh chives if desired.