Bring a large pot of salted water to a boil. (If you’re planning to bake the macaroni and cheese in a baking dish instead of a skillet, butter 9-by-13-inch baking dish or other 3-quart casserole.)
Meanwhile, in a deep, large (12-inch) ovenproof skillet, melt 2 tablespoons butter over medium-high heat. Add the onions, thyme sprigs and bay leaf, if using, and season with salt and pepper. Cover with a lid, baking sheet or foil and cook, stirring once or twice, until the onions are softened, 3 to 5 minutes.
Uncover and continue to cook on medium-high, stirring occasionally, until deep golden brown, 20 to 25 minutes. If the onions look dry, add a few tablespoons of water at a time to prevent them from burning, scraping up any browned bits that are stuck to the bottom of the skillet. (You will need to do this several times.)
While the onions are cooking, heat the oven to 450 degrees. Cook the pasta in the boiling water until 2 minutes shy of al dente; drain and set aside. Rub one side of each baguette slice with garlic.
When the onions are a deep golden brown, discard the thyme sprigs and bay leaf and deglaze the skillet with the vinegar until evaporated, scraping up browned bits as you go, about 30 seconds. Add the remaining 2 tablespoons butter. When melted, add the flour and cook, stirring, until the flour begins to stick to the bottom of the pan and has turned a light golden brown, about 3 minutes. Slowly whisk in the milk and season with salt and pepper. Bring to a simmer, whisking often.
Reserve 1 cup of the Gruyère. Carefully add the remaining Gruyère and all the Cheddar to the caramelized onion mixture and carefully stir until melted. If your pan is big enough, add the cooked pasta and stir to combine, or combine the sauce and pasta in the prepared dish.
Spread the pasta mixture in an even layer in the prepared dish, then top with the baguette slices, garlic-side up. Sprinkle the toasts with the reserved 1 cup Gruyère and season with pepper. Place the skillet or dish on a sheet pan and bake until bubbly and brown in spots, 10 to 15 minutes. If you like a crispier top, broil for a few minutes. Let cool slightly, then garnish with fresh thyme leaves.