Step 2: Make the White Sauce
In a medium saucepan over medium-low heat, melt the butter. Add the diced onion and cook, stirring occasionally, for 5 to 7 minutes until the onion is completely softened and translucent but not browned. Add the flour and stir constantly for 1 to 2 minutes — this cooks the flour and removes the raw taste that would otherwise linger in the finished sauce. The mixture will look like a pale paste; this is correct. Add the dried thyme, salt, and pepper and stir to combine.
Begin adding the milk gradually — add about a quarter cup at a time, whisking constantly after each addition to keep the sauce smooth. Once all the milk is incorporated, add the chicken broth and whisk to combine. Increase the heat to medium and bring the sauce to a gentle boil, whisking frequently. Once it reaches a boil, cook and stir for 2 minutes until the sauce has thickened enough to coat the back of a spoon. Remove from heat. If adding cheese, stir in half the shredded cheddar now until melted and smooth; reserve the other half for the top.
Step 3: Layer the Casserole
Drain the potato slices and pat them as dry as possible with a clean kitchen towel — excess moisture will thin the sauce during baking. Place one-third of the potato slices in an even layer across the bottom of the prepared baking dish, overlapping slightly. Scatter one-third of the ham pieces over the potatoes. Pour one-third of the white sauce evenly over the ham and potatoes. Repeat this sequence — potatoes, ham, sauce — for two more layers, finishing with the remaining sauce on top and ensuring it covers the surface completely so the potatoes do not dry out during baking. Sprinkle the reserved half cup of shredded cheddar over the top if using.
Step 4: Bake
Cover the baking dish tightly with aluminum foil. Bake covered at 350°F for 45 to 60 minutes, until the potatoes in the center of the dish are beginning to soften when tested with a fork or thin knife. Remove the foil and continue baking for an additional 20 to 30 minutes until the top is golden brown and bubbling at the edges and the potatoes are completely fork-tender throughout. The total covered plus uncovered baking time is typically 65 to 90 minutes; the variation depends on the thickness of your potato slices, the depth of your baking dish, and how cold the assembled casserole was when it went into the oven. Begin checking for doneness at 65 minutes by inserting a thin knife into the center — it should slide through without resistance.