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 during the last minute.
03 - Combine cream, milk, thyme, nutmeg, black pepper, and remaining 1 tbsp butter in a small saucepan. Heat over medium-low until steaming, without boiling. If using flour, whisk in until smooth to slightly thicken.
04 - Arrange half of the sweet potato slices in the prepared baking dish. Season lightly with salt.
05 - Spread half of the caramelized onions over the potatoes. Sprinkle with half of the Gruyère and cheddar cheeses.
06 - Repeat with remaining sweet potatoes, caramelized onions, and cheeses.
07 - Pour the warm cream mixture evenly over the layered ingredients.
08 - Cover tightly with aluminum foil. Bake for 40 minutes.
09 - Remove foil and continue baking for 20 minutes, until top is golden brown and potatoes are fork-tender.
10 - Let the gratin rest for 10 minutes before serving to set. Garnish with fresh chives if desired.