Using a filtered index when setting a variable
The optimization that allows you to use the filtered index with a RECOMPILE hint is called the &quot;parameter embedding optimization.&quot;  That's a process where the query parser replaces the variable reference with the literal value inside the variable.
See this post from Paul White for the reason why it doesn't work in your second case: Parameter Sniffing, Embedding, and the RECOMPILE Options

There is one scenario where using OPTION (RECOMPILE) will not result in the parameter embedding optimization being applied. If the statement assigns to a variable, parameter values are not embedded: