Thinly slice the potatoes using a sharp knife or a mandolin.
Preheat the oven to 375 F and lightly grease a 9 x 13 baking dish.
Sauté the onions in melted butter in a large saucepan over medium heat for about 5 minutes. Add the garlic and sauté a minute more, then whisk in the flour. Cook to toast the flour, whisking regularly, for about 2 to 3 minutes.
Whisk in the milk, and cook to let thicken, about 5 minutes. Stir occasionally to make sure nothing sticks to the bottom. Add the thyme as well as desired salt and pepper.
Layer about half the potatoes in the bottom of the baking dish. Top with about half the ham and half the cheese. Pour on half of the sauce, then repeat to use everything up.
Cover with foil and bake for one hour. Remove foil, then return to the oven to bake another 20 minutes, or until the cheese at the top is golden brown and the whole dish is bubbly.