Drain and discard 1 cup of the juice from one of the cans of tomatoes. Put the tomatoes and the remaining juices in a blender or food processor and purée until smooth. Set aside.
Position a rack in the center of the oven and heat the oven to 450°F. Put a large pot with 6 quarts water and 2 Tbs. kosher salt on to boil. Grease a 9x13-inch baking dish with 1 Tbs. of the olive oil.
Heat the remaining 2 Tbs. olive oil in a 6- to 8-quart Dutch oven or heavy-based pot over medium-high heat. When the oil is shimmering, about 1 minute, add the onions and 1/4 tsp. kosher salt. Cook, stirring occasionally, until almost cooked through, about 5 to 10 minutes.
Using a spatula or wooden spoon, push the onions to the side. If the pan is dry, add 1 Tbs. olive oil, and then the garlic, and cook until it just starts to sizzle and becomes fragrant, about 10 seconds.
Add the puréed tomatoes plus 1 tsp. kosher salt. Bring to a boil over medium-high heat, reduce to a gentle simmer, and cook for 10 minutes, stirring occasionally, so that the sauce thickens slightly.
Add the rosemary and chile flakes and cook until they’re heated through and their flavors have melded, about 5 minutes. Taste for salt and pepper.
Cook the pasta in the boiling salted water until it’s just tender to the tooth, about 10 minutes. (Cook the pasta a little less than you normally might, because it will cook more in the oven later.)Drain the pasta and add it to the sauce.
Add half each of the Fontina, Pamigiano-Reggiano, and Mozzarella to the pasta mixture and toss well. Transfer to the baking dish and spread evenly.
Top the pasta with the remaining cheese. Bake until the cheese is golden brown, about 15 minutes. Let rest for 10 minutes before serving.