Place onions in a 13 x 9-inch baking dish coated with cooking spray.
Drizzle with oil, tossing to coat.
Bake at 400 for 40 minutes, stirring halfway through cooking time.
Remove from oven.
Drizzle sherry over onions; stir to combine.
Bake an additional 40 minutes, stirring once.
Melt butter in a small saucepan over medium heat.
Add flour, stirring with a whisk until smooth. Gradually add milk, stirring with a whisk until blended; bring to a boil. Cook 1 minute, stirring constantly.
Remove from heat.
Add cheese, salt, and pepper, stirring until smooth.
Pour milk mixture over onions, stirring to combine.
Bake at 400 for 20 minutes or until mixture is thick and beginning to brown on top.