2 ounces (4 tablespoons) unsalted butter; more for the baking dish
2 pounds yellow onions, thinly sliced (about 6 cups)
1/2 teaspoon kosher salt; more to taste
1/2 teaspoon freshly ground black pepper, more to taste
2 cups heavy cream
3 sprigs fresh thyme
1/2 tablespoon freshly grated orange zest (from 1 orange)
1/8 teaspoon cayenne
4 pounds sweet potatoes (about 5 medium)
1 cup (4 ounces) pecan halves, toasted
2 tablespoons fresh breadcrumbs
2 tablespoons freshly grated Parmigiano-Reggiano
Cook the onions:
Heat the butter in a heavy-based 12-inch skillet over medium heat until it begins to foam. Add the onions, reduce the heat to medium low, and cook slowly, stirring occasionally, until the onions are soft and nicely browned, 30 to 40 minutes. Season with the 1/2 tsp. each salt and pepper. Remove from the heat and set aside to cool slightly.
Assemble the gratin:
Meanwhile, put the heavy cream, thyme, orange zest, and cayenne in a 2- to 3-quart saucepan. Bring to a boil, remove from the heat, and steep for 15 minutes. Remove and discard the thyme sprigs.
While the cream is steeping, peel and cut the sweet potatoes crosswise into 1/8-inch-thick slices (use a mandoline if you have one).
Position a rack in the center of the oven and another rack directly below. Heat the oven to 350°F. Lightly butter a 9x13-inch baking dish. Arrange about one-third of the sliced sweet potatoes in a double layer on the bottom of the dish, slightly overlapping the slices in each layer. Season lightly with salt and pepper. Spread half of the onions over the potatoes and drizzle about one third of the cream (2/3 cup) over the onions. Arrange another third of the potatoes in two more overlapping layers and season lightly with more salt and pepper. Spread the remaining onions over the potatoes and drizzle another third of the cream over the onions. Use the remaining sweet potato slices to make two final layers. Press down on the layers with your hands to compact and flatten them. Season lightly with salt and pepper, and drizzle the remaining cream over the potatoes, trying to cover them as much as possible.
Put a foil-lined baking sheet on the lower rack to catch any drips. Cover the gratin tightly with foil and bake on the center rack until the potatoes are almost tender but still offer a little resistance when pierced with a fork or skewer, about 1 hour. Remove the foil and bake until the sweet potatoes are completely tender and the top is lightly browned and bubbly, 30 to 40 minutes.
While the gratin is baking, put the pecans, breadcrumbs, and Parmigiano in a food processor and pulse until coarsely chopped. Raise the oven temperature to 375°F. Cover the top of the gratin with the pecan mixture and return the baking dish to the oven. Bake until the top is lightly browned, about 10 minutes.
Make Ahead Tips
The onions can be made up to 3 days in advance. Transfer them to a bowl, cover with plastic, and refrigerate. The gratin can be baked up to 1 day ahead to the point of adding the breadcrumb topping. Reheat at 375°F until bubbling and hot throughout, about 20 minutes, and then add the topping and bake 10 minutes more.