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, for 20-25 minutes until deeply golden and caramelized. Add minced garlic during the final minute and remove from heat.
03 - Combine heavy cream, whole milk, thyme, nutmeg, black pepper, and remaining 1 tbsp butter in a small saucepan. 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. Spread half the caramelized onions over potatoes, then sprinkle with half the Gruyère and cheddar cheeses. Repeat layers with remaining potatoes, onions, and cheeses.
05 - Pour the warm cream mixture evenly over the layered ingredients. Cover tightly with aluminum foil and bake for 40 minutes.
06 - Remove foil and continue baking for 20 additional minutes until top is golden brown and potatoes are fork-tender.
07 - Allow gratin to rest for 10 minutes before serving. Garnish with fresh chopped chives if desired.