This is similar to Phelype's comment (which I see just now) but it uses the (square root of the) Jacobian of the current transformation matrix to determine the scale factor.
\documentclass{article}
\usepackage{tikz}
% Jacobians have already been used in https://tex.stackexchange.com/q/86897/138900
% https://tex.stackexchange.com/a/496418 and https://tex.stackexchange.com/a/506249/194703
\makeatletter
\tikzset{scale line widths/.style={%
/utils/exec=\pgfgettransformentries{\tmpa}{\tmpb}{\tmpc}{\tmpd}{\tmp}{\tmp}%
\pgfmathsetmacro{\myJacobian}{sqrt(abs(\tmpa*\tmpd-\tmpb*\tmpc))}%
\pgfmathsetlength\pgflinewidth{\myJacobian*0.4pt}%
\def\tikz@semiaddlinewidth##1{\pgfmathsetmacro{\my@lw}{\myJacobian*##1}%
\tikz@addoption{\pgfsetlinewidth{\my@lw pt}}\pgfmathsetlength\pgflinewidth{\my@lw pt}},%
thin}}
\makeatother
\begin{document}
\subsubsection*{Original}
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (1,1);
\draw[line width=2pt] (0,1) -- (1,0);
\end{tikzpicture}
\subsubsection*{Just scale}
\begin{tikzpicture}[scale=4]
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (1,1);
\draw[line width=2pt] (0,1) -- (1,0);
\end{tikzpicture}
\subsubsection*{Scale and \texttt{scale line widths}}
\begin{tikzpicture}[scale=4,scale line widths]
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (1,1);
\draw[line width=2pt] (0,1) -- (1,0);
\typeout{\the\pgflinewidth}
\end{tikzpicture}
\end{document}

(Yes, I also see that the line joins are not nice, but this is not the point of this post. ;-)
And this is a version that locally retrieves the scale factor.
\documentclass{article}
\usepackage{tikz}
% Jacobians have already been used in https://tex.stackexchange.com/q/86897/138900
% https://tex.stackexchange.com/a/496418 and https://tex.stackexchange.com/a/506249/194703
\makeatletter
\tikzset{scale line widths/.style={%
/utils/exec=\def\tikz@semiaddlinewidth##1{%
\pgfgettransformentries{\tmpa}{\tmpb}{\tmpc}{\tmpd}{\tmp}{\tmp}%
\pgfmathsetmacro{\myJacobian}{sqrt(abs(\tmpa*\tmpd-\tmpb*\tmpc))}%
\pgfmathsetlength\pgflinewidth{\myJacobian*0.4pt}%
\pgfmathsetmacro{\my@lw}{\myJacobian*##1}%
\tikz@addoption{\pgfsetlinewidth{\my@lw pt}}\pgfmathsetlength\pgflinewidth{\my@lw pt}},%
thin}}
\makeatother
\begin{document}
\subsubsection*{Original}
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (0,1);
\draw[line width=2pt] (1,0) -- (1,1);
\begin{scope}[scale=2]
\draw[line width=2pt] (1,0) -- (1,0.5);
\end{scope}
\end{tikzpicture}
\subsubsection*{Just scale}
\begin{tikzpicture}[scale=4]
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (0,1);
\draw[line width=2pt] (1,0) -- (1,1);
\begin{scope}[scale=2]
\draw[line width=2pt] (1,0) -- (1,0.5);
\end{scope}
\end{tikzpicture}
\subsubsection*{Scale and \texttt{scale line widths}}
\begin{tikzpicture}[scale=4,scale line widths]
\draw (0,0) rectangle (1,1);
\draw[thick] (0,0) -- (0,1);
\draw[line width=2pt] (1,0) -- (1,1);
\begin{scope}[scale=2]
\draw[line width=2pt] (1,0) -- (1,0.5);
\end{scope}
\end{tikzpicture}
\end{document}

\def\tikz@semiaddlinewidth#1{\tikz@addoption{\pgfsetlinewidth{4*#1}}\pgfmathsetlength\pgflinewidth{4*#1}}
. It should work in most of the cases, but probably not all of them. TikZ/PGF uses a TeX dimen to store the line width, so there's no way to step in and scale the line width by some value in a TeX assignment like\pgflinewidth=1pt
. If the above fails somewhere, then the solution will probably be to scale the line width directly in the driver code. – Phelype Oleinik Nov 6 '19 at 23:43