How to Make It
Melt 4 tablespoons of the butter in a large Dutch oven over medium until sizzling. Stir in onions, thyme, and 2 teaspoons of the salt. Cover and cook, stirring occasionally, until onions are softened and translucent, about 20 minutes. Reduce heat to low; continue to cook, uncovered, stirring occasionally, until onions are jammy and deeply browned, 1 hour 20 minutes to 1 hour 30 minutes. (When they start to stick and burn, add a splash of water, and continue to stir.) Stir in sherry; cook, stirring often, until liquid is absorbed, about 90 seconds. Remove from heat, and stir in 2 tablespoons of the butter until melted. Remove and discard thyme sprigs.
While onions finish cooking, place potatoes in a large pot, and add cold water to cover by 1 inch; lightly salt. Bring to a boil over high. Reduce heat to medium, and simmer, uncovered, until potatoes are fork-tender, 10 to 12 minutes. Drain well in a colander. While still very hot, pass potatoes through ricer into the pot (a potato masher or food mill will also work).
Combine half-and-half and remaining 12 tablespoons butter in a medium saucepan over medium-low; cook, stirring occasionally, until butter melts.
Return pot of potatoes to low heat. Stir in half-and-half mixture until blended. Gradually stir in 3 1/2 cups of the cheese until melted and smooth. Sprinkle with remaining 1/2 teaspoon salt.
Preheat oven to broil with oven rack about 6 inches from heat. Spoon half of the mashed potato mixture into a lightly greased 13- x 9-inch baking dish. Spread 1 1/2 cups of the caramelized onions in an even layer on top. Top with remaining mashed potato mixture. Spread remaining 1/2 cup caramelized onions on top of mashed potatoes. Sprinkle with remaining 1/2 cup cheese. Broil until cheese is melted and browned in spots, 5 to 7 minutes. Garnish with fried onions and thyme leaves.