Preheat the oven to 375 degrees. Lightly grease a gratin dish or medium casserole.
Cut off the root and tip of the onions. Slice in half length-wise and peel. Lay the cut-side of the onion down and slice length-wise into thick wedges.
In a Dutch oven or heavy pot, melt two tablespoons of butter over medium heat. Add the onions and cook, stirring occasionally, until soft and caramelized, about 40 minutes. Add the sherry and simmer until it evaporates. Remove the onions from the pan and pour through a fine mesh sieve. Let them drain while making the bechamel sauce. (Draining the onions prevents a runny gratin because the onions will continue to release more liquid in the oven.)
In the same Dutch oven, melt 3 tablespoons of butter over medium heat. Stir in the flour and cook for one minute. Add the scalded milk and cook, whisking constantly, until the sauce is very thick. Remove from the heat. Fold in the Gruyere and stir until completely melted. Season with salt and pepper, to taste. Add the onions to the cheese sauce and toss to thoroughly coat. Pour the onion mixture into the gratin dish.
Melt the remaining tablespoon of butter and toss with the panko crumbs. Sprinkle over the top of the onions and bake until bubbly and golden brown, about 30 minutes. Serve warm.