Blanch the green beans by cooking in boiling water for 3 minutes.
Drain and transfer to a bowl of ice water. Drain and pat dry.
Preheat the oven to 375 degrees
In a large skillet melt the ghee over medium-high heat. Add the mushrooms and onions and cook for 10 minutes.
Add broth, heavy cream, cream cheese, thyme, salt, pepper, and xanthan gum.
Bring to a boil and then reduce to a simmer. Cook for 5 minutes, until thickened.
Add the beans to the skillet and toss to combine.
Spray a baking dish with oil and add the green beans to the dish.
Top with cheese and cook for 20 minutes.
If using the shallot topping, add it to the top of the cheese and cook for 5 more minutes to warm through.
Heat oil in a small skillet over medium high heat. Working in batches if necessary to avoid overcrowding, add the shallots and cook for 2 minutes, until golden brown.
Remove from heat and place on a plate lined with a paper towel. Repeat with remaining shallots.
Add to the top of the casserole.