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 with a pinch of salt. Cook stirring frequently until deeply golden and caramelized, about 20-25 minutes. Add minced garlic and cook 1 minute more.
03 - In a small saucepan, combine heavy cream, whole milk, dried thyme, nutmeg, black pepper, and remaining 1 tbsp butter. Heat over medium-low until steaming but not boiling. If using flour, whisk in until smooth and slightly thickened.
04 - Arrange half the sweet potato slices in the prepared baking dish. Season lightly with salt.
05 - Spread half the caramelized onions over the potatoes. Sprinkle with half the Gruyère and half the cheddar cheese.
06 - Repeat with remaining sweet potatoes, caramelized onions, and cheeses.
07 - Pour the warm cream mixture evenly over the entire dish, ensuring it penetrates the layers.
08 - Cover tightly with aluminum foil. Bake for 40 minutes.
09 - Remove foil and continue baking uncovered for 20 minutes until top is golden brown and potatoes are fork-tender.
10 - Let gratin rest for 10 minutes before serving. Garnish with fresh chives if desired.