Preheat oven to 450°F. Melt 3 tablespoons of butter in a large skillet or Dutch oven over medium heat. Add the onions and cover, allowing to steam and soften, about 8 minutes. Uncover, reduce heat to medium-low, and season with salt and pepper. Continue cooking, stirring occasionally, until onions are deeply caramelized, between 45 minutes to an hour and 15 minutes.
Once onions are caramelized, add vinegar to deglaze the pan. Allow the liquid to evaporate, then remove from heat. Caramelized onions can be stored in the refrigerator for a few days; bring to room temperature before use.
Melt remaining butter in a medium skillet over medium heat. Add breadcrumbs and thyme and toast, stirring frequently, until lightly golden. Remove from heat, season with salt and pepper, and set aside. Once cool, add 1⁄2 cup Comté and red pepper flakes, mixing well.
Add beans and stock to the caramelized onions, stirring well to combine. Pour half of the onion-bean mixture into a baking dish. Evenly sprinkle the remaining Comté on top, then evenly spread the remaining bean-onion mixture on top of the Comté layer. Top with breadcrumbs and bake for 12 to 15 minutes, or until deeply browned and bubbling at the edges.