Here is the criterion:
Lemma. Let $X$ be a regular variety, and let $U \subsetneq X$ be a nonempty open subset such that $\operatorname{codim}(X - U) \geq 2$. Then the following are equivalent:
- Every vector bundle on $U$ extends to a vector bundle on $X$;
- Every reflexive sheaf on $X$ that is locally free on $U$ is locally free;
- $\dim(X) \leq 2$.
Proof. 1 $\Rightarrow$ 2: if $\mathscr F$ is reflexive and $\mathscr F|_U$ is locally free, then $\mathscr F|_U$ extends to a vector bundle $\mathscr E$ on $X$. Since extensions of reflexive sheaves are unique [Tag 0EBJ], we conclude that $\mathscr F = \mathscr E$, i.e. $\mathscr F$ is locally free.
2 $\Rightarrow$ 1: if $\mathscr F$ is locally free on $U$, then its unique reflexive extension $j_*\mathscr F$ is locally free by assumption.
3 $\Rightarrow$ 2: see [Tag 0B3N].
2 $\Rightarrow$ 3: pick a closed point $x \in X - U$. Since $X$ is regular, there exists a locally free sheaf $\mathscr E$ and a surjection $\mathscr E \twoheadrightarrow \mathcal I_x$ [Tag 0F8A]. Its kernel $\mathscr F$ is reflexive [Tag 0EBG], and we get an exact sequence
$$0 \to \mathscr F \to \mathscr E \to \mathcal O_X \to \mathcal O_x \to 0.\tag{1}\label{1}$$
Restricting to $U$ gives a short exact sequence
$$0 \to \mathscr F|_U \to \mathscr E|_U \to \mathcal O_U \to 0,$$
showing that $\mathscr F|_U$ is locally free. By assumption, this implies that $\mathscr F$ is locally free. Then \eqref{1} shows that $\mathscr Ext^i(\mathcal O_x,\mathscr G) = 0$ for any $i > 2$ and any coherent sheaf $\mathscr G$, so in particular $\operatorname{depth}(\mathcal O_{X,x}) \leq 2$. Since $X$ is regular, this means $\dim_x X \leq 2$. Since $X$ is integral and of finite type over a field, it is equidimensional, so $\dim_x X = \dim X$ since $x$ is closed. $\square$