Heat 1/2" oil in a heavy skillet over medium-high heat (you'll know it's ready when you slide a test onion into the oil and it bubbles up vigorously). Toss onion with flour and, using a slotted spoon, slide half of them into the hot oil (always slide, never drop things into the oil). Fry until golden brown, about 5 minutes, watching closely. Remove with a slotted spoon and drain on a paper towel-lined plate. Sprinkle immediately with salt. Repeat with remaining onions.
To make casserole:
Preheat oven to 350ºF. Butter a 1 1/2 litre/quart baking dish and set aside.
Cook the green beans until crisp-tender in a pot of salted boiling water, about 6 minutes. Plunge in an ice bath (big bowl of ice and water) to stop the cooking.
Melt butter in a medium saucepan over medium heat; add onion and cook until translucent, about 5 minutes (reduce heat if browning occurs). Add garlic and cook 1 minute more. Add flour and cook 2 minutes, stirring frequently. Slowly whisk in milk. Simmer 5-10 minutes, until mixture thickens. Whisk in sour cream, and season with salt and pepper.
Drain beans and transfer to prepared baking dish; add sauce and stir to combine. Top with Mozzarella if desired. Bake 25 minutes, until hot and bubbly. Top with crispy onions and bake 5 minutes more.