Preheat the oven to 350°F (175°C). Lightly coat a 9 × 13-inch baking dish with butter or nonstick cooking spray, then place it aside.
Melt the unsalted butter in a medium saucepan set over medium-low heat. Add the finely diced onion and cook for 3–5 minutes, or until softened and translucent.
Mix the dried thyme and flour into the butter and onion. Cook for 1–2 minutes, stirring frequently, until a pale roux forms. Do not allow the mixture to brown.
Lower the heat. Pour in the milk gradually, whisking thoroughly after each addition to prevent lumps. The mixture may become quite thick at first but will loosen and smooth out as the remaining milk is incorporated.
Whisk in the chicken broth, then season the sauce with salt and ground black pepper. Increase the heat and bring the mixture just to a gentle boil. Allow it to boil for 1 minute, whisking as needed, until thickened.
Spread one-third of the sliced potatoes across the bottom of the prepared baking dish. Distribute one-third of the cubed ham over the potatoes, followed by one-third of the prepared sauce.
Repeat the potato, ham, and sauce layers twice more, finishing with the remaining sauce spread evenly over the surface.
Cover the baking dish securely with aluminum foil. Bake for 50 minutes.
Remove the foil and continue baking for another 25–35 minutes, or until the potatoes are tender when tested with a fork.
When approximately 15 minutes of baking time remain, scatter the shredded sharp cheddar evenly over the casserole. Return the dish to the oven and bake until the cheese is fully melted.
Remove the casserole from the oven and let it stand for at least 15 minutes before cutting and serving.