Preheat the oven to 425°F (218°C) and adjust the rack to the upper third of the oven.
In a deep 14-inch (35-cm) ovenproof skillet or a Dutch oven over medium-high heat, warm the oil until shimmering, 2 to 4 minutes.
Add the ground beef or sausage and cook, breaking up large pieces with a spoon, until browned, 3 to 5 minutes.
Toss in the garlic, onion, rosemary, and red pepper flakes and cook, stirring occasionally, until the onion is translucent, about 5 minutes.
Stir in the tomato paste and cook, stirring, until it darkens slightly, 1 to 2 minutes.
Add the canned tomatoes and their juices, water, and salt. Cover and bring to a boil. Add the spaghetti and, using a wooden spoon, turn and stir the spaghetti in the liquid until it begins to soften, about 5 minutes.
Reduce the heat to a simmer and continue to cook, stirring frequently, until the pasta is al dente, 8 to 10 minutes, stirring to ensure the pasta cooks evenly and doesn’t stick to the skillet.
Remove the skillet from the heat. Stir in the Pecorino Romano cheese and then sprinkle the mozzarella on top. Bake until the cheesy baked spaghetti is bubbling and browned in spots, 10 to 12 minutes. If the cheese isn’t browned, you can crank up the broiler and slide it underneath that for about 1 minute.
Let rest for 5 minutes before serving.